//工具方法
import {
    routerConfig
} from "@/router";

var util = {};

util.windowHeight = function () {
    var height = null;
    if (window.innerHeight) {
        height = window.innerHeight;
    } else if (document.body && document.body.clientHeight) {
        height = document.body.clientHeight;
    } else {
        height = 0;
    }
    return height;
};

util.cancelModal = function (...params) {
    var [vm, modal, form] = params;
    vm[modal] = false;
    vm.$refs[form].resetFields();
};

util.getCurrentTitle = function(name) { //获取当前标题
    // console.log(name)
    var title = null;
    var result = routerConfig.filter(value => {
        return value.name === name;
    });
    title = result[0].title;
    return title;
};

util.getCurrentPath = function (name) { //获取当前路由位置
    // console.log(name)
    var currentPath = [];

    var result = routerConfig.filter(value => {
        return value.name === name;
    });

    if (name === "home") {
        currentPath = [{
            path: "",
            name: "home",
            title: "首页",
        }]
    } else {
        currentPath = [{
            path: "/",
            name: "home",
            title: "首页",
        }, {
            path: "",
            name: result[0].name,
            title: result[0].title
        }]
    }
    return currentPath;
};

util.getArrKeys = function (arr) {
    var newArr = [];
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].children && arr[i].children.length > 0) {
            this.getArr(arr[i].children);
        } else {
            arr[i].checked = arr[i].check;
        }
    }
    return arr;
};

util.getCheckArr = function (arr) {
    return arr.map(item => {
        return item.id;
    });
};

util.changeDateValue = function (date, value) {
    var result = "";
    var _date = date.split(' ');
    _date[1] = value;
    return _date.join(' ')
}

export default util;
